﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PLCTest
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {

            try
            {
                //连接后，返回的结果
                int iReturnCode;
                // ActUtl类型的本地站点号
                int iLogicalStationNumber = Convert.ToInt32(this.textBox1.Text);

                //不加这三句会报
                //引发类型为“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常
                ((System.ComponentModel.ISupportInitialize)(axActUtlType)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(axActUtlType)).EndInit();
                axActUtlType.ActLogicalStationNumber = iLogicalStationNumber;

                //设置口令
                axActUtlType.ActPassword = "";

                //执行打开口令，并将返回值赋给iReturnCode
                iReturnCode = axActUtlType.Open();

                //通过执行的返回值来判断后续执行
                //如果执行成功，则将站点号的textbox使能为0
                //并且后续执行此类型操作
                if (iReturnCode == 0)
                {
                    MessageBox.Show("连接PLC成功！");
                }
                else
                {
                    MessageBox.Show("连接PLC失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                axActUtlType.Close();
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                int iReturnCode;
                short[] arrDviceValue = new short[1];
                arrDviceValue[0] = Convert.ToInt16(this.textBox3.Text);
                iReturnCode = axActUtlType.WriteDeviceRandom2(this.textBox2.Text + "\n", 1, ref arrDviceValue[0]);
                MessageBox.Show(String.Format("0x{0:x8}", iReturnCode));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            try
            {
                int readData;
                axActUtlType.ReadDeviceRandom(this.textBox2.Text, 1, out readData);
                MessageBox.Show(readData.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //short[] MX = new short[2];
            //MX[0] = 100;
            //MX[1] = 100;
            //byte[] L = System.BitConverter.GetBytes(MX[0]);
            //byte[] H = System.BitConverter.GetBytes(MX[1]);
            //byte[] f = new byte[H.Length + L.Length];
            //Buffer.BlockCopy(L, 0, f, 0, L.Length);
            //Buffer.BlockCopy(H, 0, f, L.Length, H.Length);
            //float FF = BitConverter.ToSingle(f, 0);

            float ss = 32.000f;
            string t = FloatToHex(ss);
            string q = t.Substring(0, 16);
            string h = t.Substring(16, 16);

            int a = Convert.ToInt32(q.Replace(" ",""), 2); //二进制字符串转整数

            int b = Convert.ToInt32(h.Replace(" ", ""), 2); //二进制字符串转整数
        }

        private static string FloatToHex(float value)
        {
            byte[] bytes = BitConverter.GetBytes(value);
            StringBuilder stringBuffer = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                if (i != 0)
                {
                    stringBuffer.Insert(0, " ");
                }
                string hex = Convert.ToString(bytes[i], 2);
                stringBuffer.Insert(0, hex);
                // 位数不够补0
                for (int j = hex.Length; j < 8; j++)
                {
                    stringBuffer.Insert(0, "0");
                }
            }
            return stringBuffer.ToString();

        }
    }
}